package com.cy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class NacosConfigApplication {
    //对日志进行处理都使用这个API
    //通过这个可以向控制台、文件输出日志信息
    private static Logger logger =
            LoggerFactory.getLogger(NacosConfigApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
//        logger.debug("==debug==");
//        logger.info("==info==");
//        logger.error("==error==");
    }
    @RefreshScope //支持配置动态刷新，是对spring的scope的升级版
    @RestController
    @RequestMapping("/config")
    public class NacosConfigController{
        /**
         * 这里的error表示若没有取到级别则给一个默认值
         * 这里仅在系统初始化时进行赋值，需要支持配置动态刷新
         */
        @Value("${logging.level.com.cy:error}")
        private String loglevel;
        @Value("${tomcat.threads.max:200}")
        private Integer threadMax;
        @Value("${page.pageSize:10}")
        private Integer pageSize;
        @GetMapping("/page/size")
        public String getPageSize(){
            return "page size is "+pageSize;
        }
        @GetMapping("/threads/max")
        public String getThreadsMax(){
            return "Threads max is "+threadMax;
        }
        @GetMapping("/log/level")
        public String doPrintLogLevel(){
            System.out.println("log.level="+loglevel);
            logger.error("==logger.error==");
            logger.info("==logger.info==");
            logger.debug("==logger.debug==");
            return "logging level is "+loglevel;
        }
    }
}
